package it.uniroma2;

import it.uniroma2.cloud.PROVIDER;
import it.uniroma2.cloud.ProviderFactory;
import it.uniroma2.cloud.util.ProviderHelperFactory;
import it.uniroma2.cloud.util.Utils;
import it.uniroma2.jcloudstests.Command;

import java.io.File;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import com.yammer.metrics.Counter;
import com.yammer.metrics.Meter;
import com.yammer.metrics.MetricFilter;
import com.yammer.metrics.MetricRegistry;
import com.yammer.metrics.Timer;
import com.yammer.metrics.graphite.Graphite;
import com.yammer.metrics.graphite.GraphiteReporter;

public class Executor implements Runnable{

	private Workload workload;
	private String targetUrl;
	private boolean controller;
	
	private static final MetricRegistry mRegistry = new MetricRegistry(Utils.getIP()+ "-client");
	private final Timer timer = mRegistry.timer("duration");
	private final Counter localRequestCounter = mRegistry
			.counter(MetricRegistry.name("localRequestCounter"));
	private final Meter requestRate = mRegistry.meter("requestRate-client");

	public String toString(){
		String msg = new String();
		msg = "{";
		msg += "[workload:" + this.workload +"]," ;
		msg += "[targetUrl:" + this.targetUrl +"]," ;
		msg += "[controller:" + this.controller +"]" ;
		
		return msg;
	}
	
	public Executor(String homeDir, String targetUrl, boolean controller) {
		this.workload = new Workload(homeDir);
		this.targetUrl = targetUrl;
		this.controller = controller;
		
		
		Command awsCommand = new Command(ProviderFactory
				.createComputeService(PROVIDER.AWS_EC2), ProviderHelperFactory
				.getProviderHelper(PROVIDER.AWS_EC2));	
		final Graphite graphite = new Graphite(new InetSocketAddress(
				awsCommand.getMonitoringAddress(), 2023));
		GraphiteReporter reporter = GraphiteReporter.forRegistry(mRegistry)
				.prefixedWith(Utils.getIP() + "." + "client")
				.convertRatesTo(TimeUnit.SECONDS)
				.convertDurationsTo(TimeUnit.MILLISECONDS)
				.filter(MetricFilter.ALL).build(graphite);
		reporter.start(10, TimeUnit.SECONDS);
		
	}
	public void execRequest() {
		HttpClient client = new HttpClient();
		requestRate.mark();
		String finalUrl = null;
		if (controller){
			finalUrl = client.getDestinationUrl(targetUrl);		
		}else
			finalUrl = targetUrl;
		//System.out.print(".");
		final Timer.Context context = timer.time();
		File f = workload.randomPick();
		client.upload(finalUrl, f);
		context.stop();
		localRequestCounter.inc();
	}
	
	
	public void run() {
		execRequest();
	}

	public static void main(String[] param) throws UnknownHostException,
			InterruptedException {
		
		

		
//		Executor ex = new Executor(home, "http://ec2-54-224-80-100.compute-1.amazonaws.com:8080/imagetranscoder/uploadServlet", false);
//		FixedRate fixedRate = new FixedRate(80, ex);
		
		
//		FixedRate fixedRate = new FixedRate(Double.parseDouble(param[0]), new Executor(param[1], param[2], Boolean.parseBoolean(param[3])));
	}
}
